<template>
	<cu-custom :isBack="true">
		<template #title>查看报告</template>
	</cu-custom>
	
	<view class="theme-shadow mb-3">
		<view class="report-row" v-for="(item, index) in dataList" :key="index" @click="proviewDoc(item)">
			
			<!-- <MidEllipsis :text="item.title" /> -->
			<text class="text-ellipsis-1 text-blue">
				<text class="id-row"></text>
				{{ item.title }}
			</text>
			<text class="cuIcon-right text-gray"></text>
		</view>
	</view>
	<view v-if="dataList.length === 0" class="empty-hint">
		<image src="../../static/images/empty-message.png" mode="aspectFit"></image>
		<view class="text-gray small-text">当前没有上传报告</view>
	</view>
	<view v-else class="text-gray text-center">
		没有更多了
	</view>
</template>

<script lang="ts" setup>
import { onLoad } from "@dcloudio/uni-app"
import { reactive, ref, type Ref } from 'vue';
import { getSubmitReport } from "@/api/mission.js"
import path from "path-browserify"
import { getSuffix } from "@/utils/base"
import MidEllipsis from "@/components/MidEllipsis.vue"
import { router } from "@/router"
import store from "@/store/index.js"

interface IReport {
	id: number
	title: string
	filepath: string
	suffix: string 
}
interface IFile {
	name: string
	url: string
}

const props = defineProps({
	mission_id: {
		type: String,
		default: null
	}
})

let mission_id = null
const dataList:Ref<IReport[]> = ref([])

const proviewDoc = (e: IReport) => {
	if (store.getters.osName === 'ios') {
		router.push({
			path: "/pages/index/word",
			params: {
				title: e.title,
				filepath: e.filepath
			}
		})
	} else {
		location.href = e.filepath
	}
}

const loadData = () => {
	getSubmitReport(mission_id)
		.then((res) => {
			dataList.value = Array.from(res, (item: IFile) => ({
				id: 0,
				title: item.name,
				filepath: item.url,
				suffix: item.url.match(/\.([0-9a-z]+)(?:[\?#]|$)/i)[1]
			}))
			// res.map((data: any) => {
			// 	let reportFile: IFile = {
			// 		name: null,
			// 		url: null
			// 	}
			// 	if (data.reportFiles) {
			// 		reportFile = JSON.parse(data.reportFiles)
			// 	}
			// 	dataList.value.push({
			// 		id: data.id,
			// 		title: (path.basename(reportFile.name) || "报告生成中"),
			// 		suffix: getSuffix(reportFile.name),
			// 		filepath: reportFile.url
			// 	})
			// })
		})
}

onLoad((options: any) => {
	if (options.mission_id) {
		mission_id = options.mission_id
		loadData()
	}
})

</script>

<style lang="stylus" scoped>
body
	background-color #fff
.report-row
	display: flex
	align-items: center
	padding: 30rpx 40rpx
	justify-content: space-between
	border-bottom: 2rpx solid #eeeeee
	background-color #ffffff
	position: relative;
	overflow: hidden
	width: 750rpx
	font-size 34rpx
	.id-row
		color: #2a2a2a
		white-space: nowrap
	.mid-ellipsis
		width: 440rpx
		color: #3974df
.report-row:active
	background-color #efefef
	
.empty-hint
	text-align: center
</style>
